package com.guigu.controller;

import com.guigu.dto.SportseventsDTO;
import com.guigu.entity.HealthPlan;
import com.guigu.entity.Sportsevents;
import com.guigu.entity.estimate;
import com.guigu.exception.SsmException;
import com.guigu.service.SportseventsService;
import com.guigu.vo.CodeGenerator;
import com.guigu.vo.Result;
import com.guigu.vo.SportseventsVO;
import com.guigu.vo.mealsVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/Sportsevents")
public class SportseventsController {
    @Autowired
    private SportseventsService spservice;

    @PostMapping("/querysp")
    public Result query(@RequestBody SportseventsDTO dto){
        try{
            List<SportseventsVO> list=this.spservice.query(dto);
            System.err.println(dto);
            if(!list.isEmpty()){
                return new Result<List<SportseventsVO>>("200", "true", "查询成功", list);
            } else if (dto.getName()!=null) {
                return new Result<List<SportseventsVO>>("200", "true", "没有对应的数据", list);
            } else{
                return new Result<List<SportseventsVO>>("400", "false", "查询失败", null);
            }
        }catch (Exception e){
            e.printStackTrace();
            throw new SsmException("异常");
        }
    }


    @PostMapping("/addsp")
    public Result add(@RequestBody Sportsevents sp){
        try{
            Integer codeid=this.spservice.maxid();

            // 如果id为null，将其默认设置为0
            if (codeid==null) {
                codeid = 0;
            }
            //生成根据id编码
            CodeGenerator generator = new CodeGenerator("YD");
            String newCode = generator.generateCode(codeid+1);
            System.out.println(newCode);
            sp.setCodes(newCode);
            System.out.println(sp);
            int id=this.spservice.add(sp);
            if(id==1){
                return new Result<>("200", "true", "添加成功", null);
            }else{
                return new Result<>("400", "false", "添加失败", null);
            }

        }catch (Exception e){
            e.printStackTrace();
            throw  new SsmException("异常");
        }
    }

    @PostMapping("/updatesp")
    public Result update(@RequestBody Sportsevents sp){
        try{
            int id=this.spservice.update(sp);
            if(id==1){
                return new Result<>("200", "true", "修改成功", null);
            }else{
                return new Result<>("400", "false", "修改失败", null);
            }
        }catch (Exception e){
            e.printStackTrace();
            throw  new SsmException("异常");
        }
    }

    @PostMapping("/deletesp/{id}")
    public Result delete(@PathVariable int id){
        try{
            int i= this.spservice.delete(id);
            if(i==1){
                return new Result<>("200", "true", "删除成功", null);
            }else {
                return new Result<>("400", "false", "删除失败", null);
            }
        }catch (Exception e){
            e.printStackTrace();
            throw  new SsmException("异常");
        }
    }
}
